using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CUtil.Excel
{
    public partial class DemoForm : Form
    {
        private CUtil.Excel.ExcelDataReader excelDataReader = null;
        private System.Data.DataSet dataSetExcel;

        public DemoForm()
        {
            InitializeComponent();
            
        }
        public System.Data.DataSet DataSetExcel
        {
            get
            {
                return this.dataSetExcel;
            }
            set
            {
                dataSetExcel = value;
            }

        }
        private void OpenAndDisplayExcel(string filename)
        {
            grpData.Enabled = false;

            excelDataReader = new CUtil.Excel.ExcelDataReader();
            excelDataReader.ReadExcelFile(filename);


            DisplayExcel();
        }
        private bool DisplayDataSet()
        {
            if (this.DataSetExcel == null)
                return false;

            cboSheet.Items.Clear();
            cboSheet.DisplayMember = "TableName";

            foreach (DataTable dt in this.DataSetExcel.Tables)
                cboSheet.Items.Add(dt);

            if (cboSheet.Items.Count == 0) 
                return false;
            grpData.Enabled = true;
            cboSheet.SelectedIndex = 0;
            return true;
        }
        private void DisplayExcel()
        {
            cboSheet.Items.Clear();
            cboSheet.DisplayMember = "TableName";

            foreach (DataTable dt in excelDataReader.DataSetExcel.Tables)
                cboSheet.Items.Add(dt);

            if (cboSheet.Items.Count == 0) return;
            grpData.Enabled = true;
            cboSheet.SelectedIndex = 0;
        }
        private void cmdLoad_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (openDialog.ShowDialog() != DialogResult.OK) return;
                
                string filename = openDialog.FileName;
                OpenAndDisplayExcel(filename);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to read file: \n" + ex.Message);
            }
        }

        private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).SelectedItem == null) return;
            grdActiveSheet.DataSource = ((ComboBox)sender).SelectedItem;
        }

        private void openDialog_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            if (!DisplayDataSet())
                System.Windows.Forms.MessageBox.Show("Please specify dataset before loading the form");


        }

        private void DemoForm_Load(object sender, EventArgs e)
        {
            DisplayDataSet();

        }

      
    }
}